#include "GCrypt.h"

using namespace std;

void OutBuf(char* buf, int buflen)
{
	int i;
	for( i=0; i<buflen; i++ )
		printf("%c",buf[i]);
	printf("\n");
}

int GetStringLength(char* str)
{
    if (!str) return 0;
    int i=0;
    do
    {
        if ( str[i] == (char)0 ) break;
        i++;
    } while (1);
    return i;
}

void OutBufU(unsigned char* buf, int buflen)
{
	int i;
	for( i=0; i<buflen; i++ )
		printf("%c",buf[i]);
	printf("\n");
}

int GetStringLengthU(unsigned char* str)
{
    if (!str) return 0;
    int i=0;
    do
    {
        if ( str[i] == (unsigned char)0 ) break;
        i++;
    } while (1);
    return i;
}

int RoundTo16(int x)
{
	return x - x%16 + 16;
}

int main()
{
	cout << "GCrypt working." << endl;

	GCrypt MrX;
	//unsigned char* Str = MrX.generate_key();
	char* Smth = "Yo wazzup niggerYo wazzup niggerYo wazzup niggerYo wazzup nigger";
	char* Smth2 = "Yo wazzup nigger";
	int StrLen = GetStringLength(Smth);
	int StrLen2 = GetStringLength(Smth2);
	unsigned char* Str = (unsigned char*)Smth;
	unsigned char* Str2 = (unsigned char*)Smth2;
	//OutBuf(Smth,StrLen);
	//OutBufU(Str,StrLen);
	
	unsigned char* Encd;
	unsigned char* EncdB;
	unsigned char* Result;
	unsigned char* Result2;
	unsigned char* Key;
	int encSize;
	int encSize2;
	
	cout << GetStringLengthU(Str) << endl;
	cout << "Generated: " << endl << Str << endl << endl;
	
	Key = MrX.generate_key();
	
	Encd = MrX.aes_encrypt(Str,StrLen,Key,&encSize);
	cout << "Encrypted: " << endl;
	OutBufU(Encd,encSize);
	////////////////
	EncdB = MrX.aes_encrypt(Str2,StrLen2,Key,&encSize2);
	cout << "Encrypted: " << endl;
	OutBufU(EncdB,encSize2);
	
	unsigned char* Encd2 = (unsigned char*)malloc(encSize);
	memcpy(Encd2,Encd,encSize);
	////////////
	unsigned char* EncdB2 = (unsigned char*)malloc(encSize2);
	memcpy(EncdB2,EncdB,encSize2);
	
	Result = MrX.aes_decrypt(Encd2,encSize,Key);
	cout << GetStringLengthU(Result) << endl;
	cout << "Decrypted: " << endl;
	OutBufU(Result,StrLen);
	////////
	Result2 = MrX.aes_decrypt(EncdB2,encSize2,Key);
	cout << GetStringLengthU(Result2) << endl;
	cout << "Decrypted: " << endl;
	OutBufU(Result2,StrLen2);
	
	return 0;
}
